﻿using System.Data.Entity;
using ThaiHoa.Win.QLSX.Entity;
using System.Data.Entity.Validation;
using System.Configuration;
using System.Data.Objects;
using System.Data;
using System.Diagnostics;
using System;
using System.Collections.Generic;

namespace ThaiHoa.Win.QLSX.QuanLyKho.UTI
{
    class QuanLyKhoDBContext : DbContext
    {

        public QuanLyKhoDBContext()
            : base("QLSX")//Gán tên csdl là QLSX
        {
            this.Configuration.LazyLoadingEnabled = false;
        }

        public QuanLyKhoDBContext(NhanVien nhanVien)
            : base("QLSX")//Gán tên csdl là QLSX
        {
            User = nhanVien;
            this.Configuration.LazyLoadingEnabled = false;
        }
        public NhanVien User { get; private set; }
        public DbSet<LoaiVatTu> LoaiVatTu { set; get; }
        public DbSet<Luoi> Luoi { set; get; }
        //public DbSet<MacDinh> MacDinh { set; get; }
        public DbSet<MacVatTu> MacVatTu { set; get; }
        public DbSet<MatHang> MatHang { set; get; }
        public DbSet<Nguoi> Nguoi { set; get; }
        public DbSet<NhanVien> NhanVien { set; get; }
        public DbSet<PhienBanDinhMucVatTu> PhienBanDinhMucVatTu { set; get; }
        public DbSet<PhienBanTonKhoToiThieu> PhienBanTonKhoToiThieu { set; get; }
        public DbSet<Ton> Ton { set; get; }
        public DbSet<TonKhoToiThieu> TonKhoToiThieu { set; get; }
        public DbSet<ThepDac> ThepDac { set; get; }
        public DbSet<ThepOng> ThepOng { set; get; }
        public DbSet<ThepTam> ThepTam { set; get; }
        public DbSet<VatTu> VatTu { set; get; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            //Set up table per type object mapping for the Visitor Hierarchy.       
            modelBuilder.Entity<LoaiVatTu>().ToTable("LoaiVatTu");
            modelBuilder.Entity<Luoi>().ToTable("Luoi");
            //modelBuilder.Entity<MacDinh>().ToTable("MacDinh");
            modelBuilder.Entity<MacVatTu>().ToTable("MacVatTu");
            modelBuilder.Entity<MatHang>().ToTable("MatHang");
            modelBuilder.Entity<Nguoi>().ToTable("Nguoi");
            modelBuilder.Entity<NhanVien>().ToTable("NhanVien");
            modelBuilder.Entity<PhienBanDinhMucVatTu>().ToTable("PhienBanDinhMucVatTu");
            modelBuilder.Entity<PhienBanTonKhoToiThieu>().ToTable("PhienBanTonKhoToiThieu");
            modelBuilder.Entity<Ton>().ToTable("Ton");
            modelBuilder.Entity<TonKhoToiThieu>().ToTable("TonKhoToiThieu");
            modelBuilder.Entity<ThepDac>().ToTable("ThepDac");
            modelBuilder.Entity<ThepOng>().ToTable("ThepOng");
            modelBuilder.Entity<ThepTam>().ToTable("ThepTam");
            modelBuilder.Entity<VatTu>().ToTable("VatTu");
        }

        public override int SaveChanges()
        {
            try
            {
                foreach (var entry in this.ChangeTracker.Entries())
                {
                    var entity = entry.Entity as MacDinh;
                    switch (entry.State)
                    {
                        case System.Data.EntityState.Added:
                            if (User != null)
                            {
                                entity.NguoiTao = User;
                                entity.NguoiSuaCuoi = User;
                            }
                            entity.TrangThai = true;
                            entity.NgaySuaLanCuoi = DateTime.Now;
                            entity.NgayTao = DateTime.Now;
                            break;
                        case System.Data.EntityState.Modified:
                            if (User != null)
                                entity.NguoiSuaCuoi = User;
                            entity.NgaySuaLanCuoi = DateTime.Now;
                            break;
                        case System.Data.EntityState.Deleted:
                            entry.Reload();
                            if (User != null)
                                entity.NguoiSuaCuoi = User;
                            entity.NgaySuaLanCuoi = DateTime.Now;
                            entity.TrangThai = false;
                            break;
                        default:
                            break;
                    }
                }
                return base.SaveChanges();
            }
            catch (System.Data.Entity.Infrastructure.DbUpdateException ex)
            {
                //var sb = new System.Text.StringBuilder();
                //foreach (var failure in ex.InnerException)
                //{
                //    sb.AppendFormat("{0} failed validation", failure.Entry.Entity.GetType());
                //    foreach (var error in failure.ValidationErrors)
                //    {
                //        sb.AppendFormat("- {0} : {1}", error.PropertyName, error.ErrorMessage);
                //        sb.AppendLine();
                //    }
                //}

                //throw new Exception(sb.ToString());
                throw ex;
            }
            //catch (DbEntityValidationException dbEx)
            //{
            //    foreach (var validationErrors in dbEx.EntityValidationErrors)
            //    {
            //        foreach (var validationError in validationErrors.ValidationErrors)
            //        {
            //            Trace.TraceInformation("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage);
            //        }
            //    }
            catch (DbEntityValidationException ex)
            {
                var sb =new System.Text.StringBuilder();
                foreach (var failure in ex.EntityValidationErrors)
                {
                    sb.AppendFormat("{0} failed validation", failure.Entry.Entity.GetType());
                    foreach (var error in failure.ValidationErrors)
                    {
                        sb.AppendFormat("- {0} : {1}", error.PropertyName, error.ErrorMessage);
                        sb.AppendLine();
                    }
                }
                throw new Exception(sb.ToString());
            }
            //}    
            return 0;
        }
        //protected override void OnModelCreating(DbModelBuilder modelBuilder)
        //{
        //    //modelBuilder.Entity<NhanVien>().HasMany(e=>e.NguoiTao).WithOptional(t=>t.df
        //}
    }
}
